462B - Appleman and Card Game - CodeForces Solution


greedy *1300

Please click on ads to support us..

Python Code:

class solution:
    def __init__(self,n,k,s):
        self.n = n 
        self.k = k
        self.s = s
        self.d ={}
        self.answer = 0
    def calculation(self):
        
        for i in s:
            if i in self.d:
                self.d[i]+=1
            else:
                self.d[i]=1
        max_num = self.k
        self.d ={k: v for k, v in reversed(sorted(self.d.items(), key=lambda item: item[1]))}
        enter = True
        for i in self.d:
            if self.d[i] <= max_num:
                self.k -= self.d[i]
                self.answer = self.answer + (self.d[i]*self.d[i])
                max_num = self.k
            else:
                self.answer = self.answer + (self.k * self.k)
                enter = False
                break
        if self.k != 0 and enter:
            self.answer = self.answer + self.k

n,k = map(int,input().split())
s = input()
root = solution(n,k,s)
root.calculation()
print(root.answer)

C++ Code:

#include <bits/stdc++.h>
 
using namespace std;

//... Gmail: [email protected]
//... Facebook: https://www.facebook.com/profile.php?id=100009621791250
//... Github: https://github.com/ahnafshahrear
 
typedef long long int i64;
typedef unsigned long long int ui64;

#define vt vector
#define pb push_back
#define Size(v) (int)(v.size())
#define Full(v) v.begin(), v.end()
#define Sort(v) sort(Full(v))
#define ReverseSort(v) sort(v.rbegin(), v.rend())
#define Reverse(v) reverse(Full(v))
#define LineBreak cout << "\n";
#define pi 3.1415926535897932384626
#define pf first
#define ps second
#define mp make_pair
#define LowerBound(v, x) lower_bound(Full(v), x) - v.begin()
// First element not less than x - zero based index
#define UpperBound(v, x) upper_bound(Full(v), x) - v.begin()
// First element greater than x - zero based index
#define Mod 1000000007;

#define Boundary(r, c, row, column) ((r >= 0 && r < row) && (c >= 0 && c < column))
//... Works with zero based index
#define Exit(msg) {cout << msg << "\n"; return;}

#define optimize() ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define fraction() cout.unsetf(ios::floatfield); cout.precision(10); cout.setf(ios::fixed,ios::floatfield);
#define file() freopen("input.txt","r",stdin); freopen("output.txt","w",stdout);

template<typename T> //... cin >> vector
istream& operator>>(istream &istream, vector<T> &v) {for (auto &it : v) cin >> it; return istream;}
template<typename T> //... cout << vector
ostream& operator<<(ostream &ostream, const vector<T> &p) {for (auto &it : p) cout << it << " "; return ostream;}

template<typename T1, typename T2> //... cin >> pair
istream& operator>>(istream &istream, pair<T1, T2> &p) {return (istream >> p.first >> p.second);}
template<typename T1, typename T2> //... cout << pair
ostream& operator<<(ostream &ostream, const pair<T1, T2> &p) {return (ostream << p.first << " " << p.second);}

template<typename T>
void _debug(T a) {
    cout << a << "]\n";
}
template<typename T, typename... Args>
void _debug(T a, Args... b) {
    cout << a << ", ";
    _debug(b...);
}
template<typename... Args>
void debug(Args... b) {cout << '['; _debug(b...);}


bool sortBySec(const pair<int,int> &a, const pair<int,int> &b)
{
    return a.second < b.second;
}

int countDigit(int number) 
{
    return floor(log10(number) + 1);
}

bool isPalindrome(string s) 
{
    for (int i = 0; i < s.length() / 2; i++) 
    {
        if (s[i] != s[s.length() - i - 1]) return false;
    }
    return true;
}

void subString(string s) 
{
    for (int i = 0; i < s.size(); i++)
    {
        for (int len = 1; len <= s.size() - i; len++)
        {
            cout << s.substr(i, len) << "\n";
        }
    }
}

// Solution starts here...

void solve()
{
    i64 size, k;
    cin >> size >> k;
    char x;
    map<char, i64> occ;
    while (size--)
    {
        cin >> x;
        occ[x]++;
    }
    vector<i64> values;
    for (auto o: occ)
    {
        values.pb(o.second);
    }
    ReverseSort(values);
    i64 ans = 0;
    for (i64 n: values)
    {
        if (n < k)
        {
            ans += n * n;
            k -= n;
        }
        else 
        {
            ans += k * k;
            break;
        }
    }
    cout << ans;
}

int main()
{
    optimize()

    int testCase = 1;
    if (false)
    {
        cin >> testCase;
    }
    for (int t = 1; t <= testCase; t++)
    {
        solve();
    }
    return 0;
}


Comments

Submit
0 Comments
More Questions

1582C - Grandma Capa Knits a Scarf
492A - Vanya and Cubes
217A - Ice Skating
270A - Fancy Fence
181A - Series of Crimes
1638A - Reverse
1654C - Alice and the Cake
369A - Valera and Plates
1626A - Equidistant Letters
977D - Divide by three multiply by two
1654B - Prefix Removals
1654A - Maximum Cake Tastiness
1649A - Game
139A - Petr and Book
1612A - Distance
520A - Pangram
124A - The number of positions
1041A - Heist
901A - Hashing Trees
1283A - Minutes Before the New Year
1654D - Potion Brewing Class
1107B - Digital root
25A - IQ test
785A - Anton and Polyhedrons
1542B - Plus and Multiply
306A - Candies
1651C - Fault-tolerant Network
870A - Search for Pretty Integers
1174A - Ehab Fails to Be Thanos
1169A - Circle Metro